package main

import (
	"fmt"
	"io"
	"net/http"
	"net/rpc"
	"net/rpc/jsonrpc"
)

type HelloService struct{}

func (h *HelloService) SayHi(greeting string, reply *string) error {
	*reply = "Hello, " + greeting
	return nil
}

func main() {
	// 1.实例化一个server 	rpc_quick_start.RegisterName("")
	_ = rpc.RegisterName("HelloService", &HelloService{})
	http.HandleFunc("/jsonrpc", func(w http.ResponseWriter, r *http.Request) {
		var conn io.ReadWriteCloser = struct {
			io.Writer
			io.ReadCloser
		}{
			ReadCloser: r.Body,
			Writer:     w,
		}

		err := rpc.ServeRequest(jsonrpc.NewServerCodec(conn))
		if err != nil {
			fmt.Println("err", err)
		}
	})

	err := http.ListenAndServe(":8088", nil)
	if err != nil {
		fmt.Println("err", err)
	}
}
